//
//  BaseNavigationController.swift
//  TQGO
//
//  Created by YXY on 2018/7/9.
//  Copyright © 2018年 Techwis. All rights reserved.
//

import UIKit

protocol SlidingDelegate : NSObjectProtocol {
    ///侧滑返回
    func slidingBack()
}

class BaseNavigationController: UINavigationController,UINavigationControllerDelegate,UIGestureRecognizerDelegate {
    
    weak var slidingDelegate : SlidingDelegate?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self;
        self.view.backgroundColor = UIColor.white
        self.interactivePopGestureRecognizer?.delegate = self
        
        if #available(iOS 15.0, *) { //UINavigationBarAppearance属性从iOS13开始
            let navBarAppearance = UINavigationBarAppearance()
            // 背景色
            navBarAppearance.backgroundColor = .white
            // 去掉半透明效果
            navBarAppearance.backgroundEffect = nil
            // 去除导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
            navBarAppearance.shadowColor = .clear
            // 字体颜色
            navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18.0)]
            
            self.navigationBar.standardAppearance = navBarAppearance
            self.navigationBar.scrollEdgeAppearance = navBarAppearance
        } else {
            self.view.backgroundColor = .white;
            self.navigationBar.shadowImage = UIImage();
            self.navigationBar.barTintColor = .white
            self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 18.0)]
        }
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: - UIGestureRecognizerDelegate
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if (self.viewControllers.count > 0) {
            ///拦截侧滑返回实现代理
            if self.slidingDelegate != nil && ((self.slidingDelegate?.responds(to: Selector.init(("slidingBack")))) != nil) {
                self.slidingDelegate?.slidingBack()
                return false;
            } else {
                return true
            }
            
        } else {
            return true;
        }
        
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return self.children.count > 1
    }
    
    @objc func clickBtnBack(_ btn:UIButton) {
        ///拦截侧滑返回实现代理
        if self.slidingDelegate != nil && ((self.slidingDelegate?.responds(to: Selector.init(("slidingBack")))) != nil) {
            self.slidingDelegate?.slidingBack()
        } else {
            self.popViewController(animated: true);

        }
    }
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
//        self.navigationBar.prefersLargeTitles = false;
//        self.navigationItem.largeTitleDisplayMode = .automatic;

//        if viewController .isKind(of: PromotionVC.self) {
//            self.navigationBar.prefersLargeTitles = true;
//            self.navigationItem.largeTitleDisplayMode = .automatic;
//        }
     
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
  
        if(viewControllers.count > 0) {
//            self.navigationBar.prefersLargeTitles = false;
//            self.navigationItem.largeTitleDisplayMode = .automatic;
//            //自定义返回按钮
            let btnBack:UIButton = UIButton(type:.custom)
//            btnBack.backgroundColor = .red
            btnBack.frame = CGRect(x:0, y:0, width:44, height:44)
            btnBack.setImage(UIImage.init(named: "icon_arrow_left"), for: .normal)
            btnBack.setImage(UIImage.init(named: "icon_arrow_left"), for: .highlighted)
            btnBack.addTarget(self, action:#selector(clickBtnBack(_ :)), for: .touchUpInside)
            btnBack.imageEdgeInsets = UIEdgeInsets(top: 0,left: -20,bottom: 0,right: 0)
//            btnBack.contentHorizontalAlignment = .left
            let backBarItem = UIBarButtonItem.init(customView: btnBack)
            viewController.navigationItem.leftBarButtonItem = backBarItem
//            // 隐藏底部tabBar
            viewController.hidesBottomBarWhenPushed = true

        }
        super.pushViewController(viewController, animated: animated)
    }
    
    //    override func popViewController(animated: Bool) -> UIViewController? {
    //        //判断即将到栈底
    //        if (viewControllers.count == 0) {
    //            //显示自定义的tabBar
    //            tabBarController?.tabBar.isHidden = false;
    //        }
    //        //  pop出栈
    //        return super.popViewController(animated: animated)
    //    }
}
